// pages/milkyTea/comment/comment.js
const app = getApp()
const tools = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    category_id: 0,
    wareList: [],
    ware_id: 0,
    wareinfo: "",
    commentList: [],
    comment_content: "",
    // 父评论输入框
    inputValue: "",
    // 用户个人信息
    userInfo: {
      avatar: '',
      user_name: '',
      user_id:''
    },
    isCanShow: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取用户登录信息
    let user_id = wx.getStorageSync('user_id');
    let user_name = wx.getStorageSync('user_name');
    let avatar = wx.getStorageSync('avatar');
    this.setData({
      userInfo: {
        avatar: avatar,
        user_name: user_name,
        user_id:user_id
      }
    });

    this.setData({
      ware_id: options.ware_id
    });
    this.checkIsCanShow();
    this.commentList(options.ware_id)
    console.log('------ware_id',options.ware_id)
    console.log(app.globalData.wareList)
    let wareList = app.globalData.wareList;  
    console.log('-----------------文章内容',wareList)   
    let wareinfo = '';
    for (let i = 0; i < wareList.length;i++){
      console.log('---------i',wareList[i].ware_id)
      if(+options.ware_id === wareList[i].ware_id){
        wareinfo = wareList[i]
      }
    }  
    this.setData({
      wareinfo: wareinfo,
    });
  },

  onSend() {
    if(!wx.getStorageSync('user_id')){
      this.wxLogin() //登录
    }else{
      let input = this.data.inputValue
      console.log("this.data.commentList",this.data.commentList)
      if (input == "" || input.trim().length == 0) {
          wx.showToast({
            title: '要输入内容噢~',
          })
      }else{
        // this.commentList = this.data.commentList,
        console.log("名称：", wx.getStorageSync('user_name'))
        console.log("内容：", this.data.inputValue)
        console.log("商品id：", this.data.wareinfo.ware_id)
        console.log("头像：", wx.getStorageSync('avatar'))
        let _this = this;
        wx.request({
          url: 'https://order.xiaxueruo.com/api/comment/add?ware_id='+this.data.wareinfo.ware_id+"&comment_content="+this.data.inputValue+"&user_name="+wx.getStorageSync('user_name')+"&avatar="+wx.getStorageSync('avatar') +'&user_id='+ wx.getStorageSync('user_id'),
          success: function(res){
            console.log(res)
            // _this.data.userInfo.avatarUrl=res.data.data.comment.avatar
            // console.log(_this.data.articleinfo.article_id)
            _this.data.commentList.unshift(res.data.data.commentList)
            _this.setData({
              commentList:_this.data.commentList,
              ware_id: _this.data.wareinfo.ware_id,
              inputValue: ""
            })
            wx.showToast({
              title: '评论成功',
              icon: 'success',
              duration: 1500,
            })
          }
        })
      } 
    }
  },


   // 在输入时获取搜索框的文本
   getInputBoxValue(e) {
    this.data.inputValue = e.detail.value;
  },

  // 在输入时获取搜索框的文本
  getRepleyInputBoxValue(e) {
    this.data.repleyInputValue = e.detail.value;
  },

  	/**
	 * 评论
	 */
  commentList(ware_id){
    let that = this
    console.log('------------9',ware_id);
    wx.request({
      url: 'https://order.xiaxueruo.com/api/comment/list?ware_id='+ware_id,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',//get或者post
      
      success: function (res) {
        console.log(".....",res)//接口返回值
        that.setData({
          commentList: res.data.data.commentList
        })
      }
    })
  },

  login: function () {
    let that = this
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log('res:', res)
        this.setData({
          userInfo: res.userInfo,
        })
        wx.login({
          success: (res) => {
            console.log('res', res)
            if (res.code) {
              wx.request({
                url: 'https://order.xiaxueruo.com/api/User/userInfo',
                data: {
                  code: res.code,
                  user_name: that.data.userInfo.nickName,
                  avatar: that.data.userInfo.avatarUrl
                },
                method: 'GET',
                success: function (res) {
                  // 接口返回openid或者一个能标识身份的标识，比如user_id
                  console.log("成功：", res.data);
                  console.log("成功：", res.data.user_id);
                  console.log("成功：", res.data.user_name);
                  console.log("成功：", res.data.avatar);

                  // 把唯一标识缓存
                  wx.setStorageSync('session3rd', res.data.session3rd)
                  wx.setStorageSync('user_id', res.data.user_id)
                  wx.setStorageSync('user_name', res.data.user_name)
                  wx.setStorageSync('avatar', res.data.avatar)

                  that.setData({
                    userInfo: {
                      avatar: res.data.avatar,
                      user_name: res.data.user_name,
                      user_id: res.data.user_id
                    },
                    isShowUserName: true
                  });
                }
              })
            }
          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取接口数据，控制是否显示敏感信息
   */
  checkIsCanShow() {
    wx.request({
      url: 'https://order.xiaxueruo.com/api/order/isCanShow/',
      success: res => {
        this.setData({
          isCanShow: res.data
        })
      }
    })
  },
})